package host.skiree;

import okhttp3.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

/**
 * Hello world!
 */
public class App {
	public static void main(String[] args) throws IOException {
		String paramString1 = "qq";
		/*
			mp3
			sq
			hq
		 */
		String type = "mp3";
		String paramString2 = "003gSjsG4eGvOJ";
		/*
			String str2 = Build.BRAND;
      		String str3 = Build.VERSION.RELEASE;
			参考 https://www.jianshu.com/p/71675f268394
		 */
		String str2 = "Redmi";
		String str3 = "11";
		long l = System.currentTimeMillis() / 1000L;
		StringBuilder stringBuilder1 = new StringBuilder();
		stringBuilder1.append("d86b856be4a7ea7a5bc9b6c4eed46f4e");
		stringBuilder1.append(l);
		stringBuilder1.append("6562653262383463363633646364306534333668");
		String str1 = d(stringBuilder1.toString()).toLowerCase();
		StringBuilder stringBuilder2 = new StringBuilder();
		stringBuilder2.append("{\\\"method\\\":\\\"GetMusicUrl\\\",\\\"platform\\\":\\\"");
		stringBuilder2.append(paramString1);
		stringBuilder2.append("\\\",\\\"t1\\\":\\\"");
		stringBuilder2.append(paramString2);
		stringBuilder2.append("\\\",\\\"t2\\\":\\\"");
		stringBuilder2.append(type);
		stringBuilder2.append("\\\"}");
		paramString2 = stringBuilder2.toString();
		StringBuilder stringBuilder4 = new StringBuilder();
		stringBuilder4.append("{\\\"uid\\\":\\\"\\\",\\\"token\\\":\\\"\\\",\\\"deviceid\\\":\\\"84ac82836212e869dbeea73f09ebe52b\\\",\\\"appVersion\\\":\\\"");
		stringBuilder4.append("4.1.4");
		stringBuilder4.append("\\\",\\\"vercode\\\":\\\"");
		stringBuilder4.append("4140");
		stringBuilder4.append("\\\",\\\"device\\\":\\\"");
		stringBuilder4.append(str2);
		stringBuilder4.append("\\\",\\\"osVersion\\\":\\\"");
		stringBuilder4.append(str3);
		stringBuilder4.append("\\\"}");

		String str5 = stringBuilder4.toString();
		StringBuilder stringBuilder5 = new StringBuilder();
		stringBuilder5.append("{\n\t\"text_1\":\t\"");
		stringBuilder5.append(paramString2);
		stringBuilder5.append("\",\n\t\"text_2\":\t\"");
		stringBuilder5.append(str5);
		stringBuilder5.append("\",\n\t\"sign_1\":\t\"");
		stringBuilder5.append(str1);
		stringBuilder5.append("\",\n\t\"time\":\t\"");
		stringBuilder5.append(l);
		stringBuilder5.append("\",\n\t\"sign_2\":\t\"");
		StringBuilder stringBuilder6 = new StringBuilder();
		stringBuilder6.append(paramString2.replace("\\", ""));
		stringBuilder6.append(str5.replace("\\", ""));
		stringBuilder6.append(str1);
		stringBuilder6.append(l);
		stringBuilder6.append("NDRjZGIzNzliNzEe");
		stringBuilder5.append(d(stringBuilder6.toString()).toLowerCase());
		stringBuilder5.append("\"\n}");
//		System.out.println(stringBuilder5);

		byte[] data  = b(a(stringBuilder5.toString().getBytes(StandardCharsets.UTF_8)).toUpperCase().getBytes(StandardCharsets.UTF_8));
//		System.out.println(data.toString());

		OkHttpClient client = new OkHttpClient().newBuilder()
				.build();
		MediaType mediaType = MediaType.parse("text/plain");
//		RequestBody body = RequestBody.create(mediaType, "x�uR[��0\f�R��᳅r�#�\u0004;m��\u000e3I�dG���q\u001b�{'���:����ks�N$��@�?nZ�\u0015ͽ�\u0003+��\u001d�\u0010?�\u001e�`�\u0003\u000e\"�+��m�e�\u000b���j�Q#�˽#����T/.�w����n��v�c��\u000e�(<��PE�S|T��\n\n�]�\bp�p\u0014�\u0010�g~\u0016M�=1V�E�5�a=z\u0014x���\u001e��Hp6��I�ɥ�C�f.\u0015$�����C3��]�_���\n��n��dMo�>��^�ț�*d��\\��BO�󚻬�\u001f\u0004�z��7��,\u001f�J�g-e��Z���=R\u0001i⒧�����\u0019\u0017�D9I�r��j#J-���\u000f\u001a<�%");
		RequestBody body = RequestBody.create(mediaType, data);
		List<String> urlList = new ArrayList<>();
		urlList.add("http://app.kzti.top/client/cgi-bin/api.fcg");
		urlList.add("http://app.kzti.top:1030/client/cgi-bin/api.fcg");
		urlList.add("http://119.91.134.171:1030/client/cgi-bin/api.fcg");
		Request request = new Request.Builder()
				.url(urlList.get(1))
//				.url("http://app.kzti.top/client/cgi-bin/api.fcg")
				.method("POST", body)
				.addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
				.addHeader("Content-Type", "text/plain")
				.addHeader("Accept", "*/*")
				.addHeader("Host", "app.kzti.top")
				.addHeader("Connection", "keep-alive")
				.build();
		Response response = client.newCall(request).execute();
		String str4 = new String(c(response.body().bytes()), StandardCharsets.UTF_8);
		System.out.println(str4);
	}

	public static String a(byte[] paramArrayOfbyte) {
		StringBuilder stringBuilder = new StringBuilder();
		int j = paramArrayOfbyte.length;
		for (int i = 0; i < j; i++) {
			stringBuilder.append(String.format("%02x", new Object[]{Byte.valueOf(paramArrayOfbyte[i])}));
		}
		return stringBuilder.toString();
	}

	public static byte[] b(byte[] paramArrayOfbyte) {
		Deflater deflater = new Deflater();
		deflater.setInput(paramArrayOfbyte);
		deflater.finish();
		paramArrayOfbyte = new byte[paramArrayOfbyte.length];
		return Arrays.copyOf(paramArrayOfbyte, deflater.deflate(paramArrayOfbyte));
	}

	public static byte[] uncompress(byte[] compressedData) {
		Inflater inflater = new Inflater();
		inflater.setInput(compressedData);

		ByteArrayOutputStream outputStream = new ByteArrayOutputStream(compressedData.length);
		byte[] buffer = new byte[1024];

		try {
			while (!inflater.finished()) {
				int count = inflater.inflate(buffer);
				outputStream.write(buffer, 0, count);
			}
			outputStream.close();
		} catch (IOException | DataFormatException e) {
			e.printStackTrace();
		}

		return outputStream.toByteArray();
	}


	public static byte[] c(byte[] paramArrayOfbyte) {
		Inflater inflater = new Inflater();
		inflater.setInput(paramArrayOfbyte);
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		byte[] arrayOfByte = new byte[1024];
		try {
			while (!inflater.finished())
				byteArrayOutputStream.write(arrayOfByte, 0, inflater.inflate(arrayOfByte));
			inflater.end();
			byteArrayOutputStream.close();
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return byteArrayOutputStream.toByteArray();
	}

	public static String d(String paramString) {
		return b.a.d(paramString);
	}

}
